package com.twu.biblioteca;

import java.util.Scanner;

public class BibliotecaApp {

    private static final String welcomeMessage = "Welcome to the Biblioteca Application";

    private static final String menuLogged =
            "-- Menu -- \n" +
            "1 - Show all Books \n" +
            "2 - Reserve a book \n" +
            "3 - Show membership details \n" +
            "4 - Show all Movies \n" +
            "\n0 - Quit Application \n" +
            "\n" +
            "Select an option:  ";

    private static final String menuNotLogged =
            "-- Menu -- \n" +
            "1 - Show all Books \n" +
            "2 - LogIn \n" +
            "3 - Show membership details \n" +
            "4 - Show all Movies \n" +
            "\n0 - Quit Application \n" +
            "\n" +
            "Select an option:  ";

    private static final String bookNameMessage = "Type the name of the Book to be reserved";

    private static final String quitMessage = "Thank you for using the App";

    private static final String invalidOptionMessage = "Select a valid option!!";

    private static final String userDetailsMessage = "Please talk to a Librarian. Thank you.";

    private static final String bookDoesNotExistMessage = "Sorry we don't have that book yet.";

    private static final String availableBookMessage = "Thank You! Enjoy the book.";

    private Biblioteca library;

    private int option;

    public BibliotecaApp(){
        library = new Biblioteca();

        // Only for Releases without concrete data
        library.addBook("Test-Driven Development");
        library.addBook("Agile Samurai");
        library.addMovie("Kill Bill", "2005", "Tarantino", "10");
        library.addUser("Diego Peleteiro", "1234567890", "dpeletei@thoughtworks.com", "brazil");
    }

    public static void main(String[] args) {
        BibliotecaApp App = new BibliotecaApp();

        App.showWelcomeMessage();

        do{
            App.showMenuMessage();
            App.setOption(App.readOptionFromConsole());
            App.treatOption();
        } while(App.getOption()!=0);
    }

    public void treatOption() {
        if(this.getOption() == 0){
            this.showQuitMessage();
        } else if (this.getOption() == 1){
            this.printBookList();
        } else if (this.getOption() == 2){
            if(loged()){
                this.reserveBook();
            }
            else{
                this.logIn();
            }
        } else if (this.getOption() == 3){
            this.getUserDetails();
        } else if (this.getOption() == 4){
            this.printMovieList();
        } else {
            this.showInvalidOption();
        }
    }

    private void logIn() {
        String User, Pwrd;
        System.out.println("Please type your Username. Format XXX-XXXX");
        Scanner stdin = new Scanner(System.in);
        User =  stdin.nextLine().replaceFirst("-", "");

        if(this.library.hasUser(User)){
            System.out.println("Please type your Password.");
            Pwrd = stdin.nextLine();
            if(tryLogIn(User, Pwrd)){
                setLoggedLibrary();
                setLoggedUserLibrary(User);
                System.out.println("Welcome! Your are logged in.");
            }
            else{
                System.out.println("Password incorrect");
            }
        }
        else{
            System.out.println("User does not exist");
        }
    }

    public void setLoggedUserLibrary(String user) {
        this.library.setLogedUser(this.library.getUserByUserName(user));
    }

    public void setLoggedLibrary() {
        this.library.setLoged();
    }

    public boolean tryLogIn(String user, String pwrd) {
        return this.library.logIn(user, pwrd);
    }

    private void printMovieList() {
        System.out.print("\nMovie\t\tYear\tDirector\tRate\n" + this.library.printMovieList());
    }

    public void reserveBook() {
        System.out.println(bookNameMessage);
        String bookName = this.getBookNameFromConsole();
        this.tryReserve(bookName);
    }

    public void printBookList() {
        System.out.println("\nList of Books available:\n" + this.library.printBookList());
    }

    public void tryReserve(String pName){
        if(library.hasBook(pName)){
            System.out.println(availableBookMessage);
        }
        else {
            System.out.println(bookDoesNotExistMessage);
        }
    }

    private String getBookNameFromConsole(){
        Scanner stdin = new Scanner(System.in);
        return stdin.nextLine();
    }

    private void getUserDetails(){
        if(loged()){
            System.out.println(getLogedUserLibrary().printUserDetails()+"\n");
        }
        else{
            System.out.println(userDetailsMessage);
        }
    }

    public User getLogedUserLibrary() {
        return this.library.getLogedUser();
    }

    private void showInvalidOption(){
        System.out.println(invalidOptionMessage);
    }

    public void showQuitMessage(){
        System.out.println(quitMessage);
    }

    public void showMenuMessage() {
        if(loged()){
            System.out.print(menuLogged);
        }
        else {
            System.out.print(menuNotLogged);
        }
    }

    public boolean loged() {
        return this.library.isLoged();
    }

    public void showWelcomeMessage() {
        System.out.println(welcomeMessage);
    }

    public int getOption() {
        return this.option;
    }

    public void setOption(int pOption){
        this.option = pOption;
    }

    private int readOptionFromConsole(){
        Scanner stdin = new Scanner(System.in);
        return Integer.parseInt(stdin.nextLine());
    }
}
